package com.share.domain;

import com.share.dbutils.QueryFactory;
import com.share.entity.Blog;
import com.share.entity.Comment;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

/**
 * Created by leeping on 2016/1/12.
 */
public class CommentDomainImpl implements CommentDomain {
	public boolean save(Comment comment, Blog blog) {
		QueryRunner queryRunner = QueryFactory.getQueryRunner();
		String sql = "insert into comment(content,createTime,userId,blogId) values (?,?,?,?)";
		ResultSetHandler handler = new BeanHandler(Comment.class);
		try {
			queryRunner.insert(sql, handler,
					comment.getContent(), comment.getCreateTime(), comment.getUserId(), comment.getBlogId());
			BlogDomain blogDomain = new BlogDomainImpl();
			if (!blogDomain.update(blog)) {
				return false;
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public List<Comment> getComments(int id) {
		QueryRunner queryRunner = QueryFactory.getQueryRunner();
		String sql = "select * from comment where blogId = ? order by createTime desc";
		ResultSetHandler handler = new BeanListHandler(Comment.class);
		try {
			List<Comment> comments = (List<Comment>) queryRunner.query(sql, handler, id);
			return comments;
		} catch (Exception e) {
			return null;
		}

	}
}
